package com.jfinal.club._admin.article;

import com.jfinal.aop.Before;
import com.jfinal.club.common.model.Article;
import com.jfinal.club.common.model.ArticleDetail;
import com.jfinal.club.index.IndexService;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.sql.SQLException;
import java.util.List;

/**
 * @Auther: 10091
 * @Date: 2018-07-14 14:15
 * @Description:文章管理业务
 */
public class ArticleAdminService {
    public final static   ArticleAdminService me = new ArticleAdminService();
    public final static Integer COMMON_DEFAULT_STATE = 0;
    private Article dao = new Article().dao();
    private ArticleDetail daoDetail = new ArticleDetail().dao();

    public Page<Article> paginate(int pageNum) {
        return dao.paginate(pageNum, 10, "select a.* ,m.media_name", "from f_article a left join f_media m on m.id = a.media_id order by a.id desc");
    }

    public Integer countArticle(String title){
        return  Db.queryInt("select id from f_article where main_title = ? ",title);
    }

    public List<Article> findAll(){
        return  dao.find("select * from f_article");
    }

    /**
     * 保存文章
     * @param article
     * @return
     */
    @Before(Tx.class)
    public Ret save(final Article article, final String details){
        if (null != article && null !=article.getMainTitle()){
            //先查询标题是否存在
            Integer articleId = Db.queryInt("select id from f_article where main_title = ? ",article.getMainTitle().trim());
            if (articleId == null){
                Db.tx(new IAtom() {
                    public boolean run() throws SQLException {
                        article.setIsTake(COMMON_DEFAULT_STATE);
                        article.setIsLock(COMMON_DEFAULT_STATE);
                        article.setIsHot(COMMON_DEFAULT_STATE);
                        article.setIsRecommend(COMMON_DEFAULT_STATE);
                        article.setIsTop(COMMON_DEFAULT_STATE);
                        article.setAuditState(COMMON_DEFAULT_STATE);
                        article.setRedNumber(COMMON_DEFAULT_STATE);
                        article.setCommentNumber(COMMON_DEFAULT_STATE);
                        article.save();
                        ArticleDetail detail = new ArticleDetail();
                        detail.setArticleId(article.getId());
                        detail.setDetail(details);
                        return  detail.save();
                    }
                });

            }else{
                return Ret.ok("msg", "文章已存在");
            }
        }else{
            return Ret.ok("msg", "参数错误");
        }

        return Ret.ok("msg", "创建成功");
    }


    /**
     * 创建
     */
    public Ret save( Article media) {
        media.setMediaName(media.getMediaName().trim());
        media.save();
        return Ret.ok("msg", "创建成功");
    }
    public Article edit(int id) {
        return dao.findById(id);
    }

    /**
     * 修改文章
     * @param article
     * @param content
     * @return
     */
    public Ret update(final Article article,final String content) {
        Db.tx(new IAtom() {
            public boolean run() throws SQLException {
                //更新文章
                article.update();
                //更新文章内容
                ArticleDetail detail =  daoDetail.findFirst("select * from f_article_detail where article_id = ?",article.getId());
                detail.setDetail(content);
                detail.update();
                return true;
            }
        });
        return Ret.ok("msg", "修改成功");
    }

    public Ret lock(int id) {
        Db.update("update f_article set is_lock = 1 where id=?", id);
        return Ret.ok("msg", "锁定成功");
    }

    public Ret unlock(int id) {
        Db.update("update f_article set is_lock = 0 where id=?", id);
        return Ret.ok("msg", "解除锁定成功");
    }

    public String getArticleDetail(int articleId){
       ArticleDetail detail =  daoDetail.findFirst("select * from f_article_detail where article_id = ?",articleId);
       if (null !=detail){
           return  detail.getDetail();
       }else {
           return "";
       }
    }

    /**
     * 删除 文章及详情
     */
    public void delete(final int articleId) {
        Db.tx(new IAtom() {
            public boolean run() throws SQLException {
                Db.update("delete from f_article_detail where article_id = ? ", articleId);
                Db.update("delete from f_article where id = ?", articleId);
                return true;
            }
        });

        IndexService.me.clearCache();
    }
}
